#include "stdafx.h"
#include "qcommandlineoption.h"

class CCommandLineOptionPrivate
{
public:
    __declspec(noinline)
    explicit CCommandLineOptionPrivate(const CString &name)
	: hidden(false)
    { 
		StringList strList;
		strList.push_back(name);
		names = removeInvalidNames(strList);
	}

    __declspec(noinline)
    explicit CCommandLineOptionPrivate(const StringList &names)
        : names(removeInvalidNames(names)),
          hidden(false)
    { }

    static StringList removeInvalidNames(StringList nameList);

    //! The list of names used for this option.
    StringList names;

    //! The documentation name for the value, if one is expected
    //! Example: "-o <file>" means valueName == "file"
    CString valueName;

    //! The description used for this option.
    CString description;

    //! The list of default values used for this option.
    StringList defaultValues;

    //! Show or hide in --help
    bool hidden;
};

/*!
    \since 5.2
    \class QCommandLineOption
    \brief The QCommandLineOption class defines a possible command-line option.
    \inmodule QtCore
    \ingroup shared
    \ingroup tools

    This class is used to describe an option on the command line. It allows
    different ways of defining the same option with multiple aliases possible.
    It is also used to describe how the option is used - it may be a flag (e.g. \c{-v})
    or take a value (e.g. \c{-o file}).

    Examples:
    \snippet code/src_corelib_tools_qcommandlineoption.cpp 0

    \sa QCommandLineParser
*/

/*!
    \fn QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)

    Move-assigns \a other to this QCommandLineOption instance.

    \since 5.2
*/

/*!
    Constructs a command line option object with the name \a name.

    The name can be either short or long. If the name is one character in
    length, it is considered a short name. Option names must not be empty,
    must not start with a dash or a slash character, must not contain a \c{=}
    and cannot be repeated.

    \sa setDescription(), setValueName(), setDefaultValues()
*/
CCommandLineOption::CCommandLineOption(const CString &name)
    : d(new CCommandLineOptionPrivate(name))
{
}

/*!
    Constructs a command line option object with the names \a names.

    This overload allows to set multiple names for the option, for instance
    \c{o} and \c{output}.

    The names can be either short or long. Any name in the list that is one
    character in length is a short name. Option names must not be empty,
    must not start with a dash or a slash character, must not contain a \c{=}
    and cannot be repeated.

    \sa setDescription(), setValueName(), setDefaultValues()
*/
CCommandLineOption::CCommandLineOption(const StringList &names)
    : d(new CCommandLineOptionPrivate(names))
{
}

/*!
    Constructs a command line option object with the given arguments.

    The name of the option is set to \a name.
    The name can be either short or long. If the name is one character in
    length, it is considered a short name. Option names must not be empty,
    must not start with a dash or a slash character, must not contain a \c{=}
    and cannot be repeated.

    The description is set to \a description. It is customary to add a "."
    at the end of the description.

    In addition, the \a valueName can be set if the option expects a value.
    The default value for the option is set to \a defaultValue.

    In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
    and later, it no longer is and can be used for C++11-style uniform
    initialization:

    \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init

    \sa setDescription(), setValueName(), setDefaultValues()
*/
CCommandLineOption::CCommandLineOption(const CString &name, const CString &description,
                                       const CString &valueName,
                                       const CString &defaultValue)
    : d(new CCommandLineOptionPrivate(name))
{
    setValueName(valueName);
    setDescription(description);
    setDefaultValue(defaultValue);
}

/*!
    Constructs a command line option object with the given arguments.

    This overload allows to set multiple names for the option, for instance
    \c{o} and \c{output}.

    The names of the option are set to \a names.
    The names can be either short or long. Any name in the list that is one
    character in length is a short name. Option names must not be empty,
    must not start with a dash or a slash character, must not contain a \c{=}
    and cannot be repeated.

    The description is set to \a description. It is customary to add a "."
    at the end of the description.

    In addition, the \a valueName can be set if the option expects a value.
    The default value for the option is set to \a defaultValue.

    In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
    and later, it no longer is and can be used for C++11-style uniform
    initialization:

    \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list

    \sa setDescription(), setValueName(), setDefaultValues()
*/
CCommandLineOption::CCommandLineOption(const StringList &names, const CString &description,
                                       const CString &valueName,
                                       const CString &defaultValue)
    : d(new CCommandLineOptionPrivate(names))
{
    setValueName(valueName);
    setDescription(description);
    setDefaultValue(defaultValue);
}

/*!
    Constructs a QCommandLineOption object that is a copy of the QCommandLineOption
    object \a other.

    \sa operator=()
*/
CCommandLineOption::CCommandLineOption(const CCommandLineOption &other)
    : d(other.d)
{
}

/*!
    Destroys the command line option object.
*/
CCommandLineOption::~CCommandLineOption()
{
}

/*!
    Makes a copy of the \a other object and assigns it to this QCommandLineOption
    object.
*/
CCommandLineOption &CCommandLineOption::operator=(const CCommandLineOption &other)
{
    d = other.d;
    return *this;
}

/*!
    \fn void QCommandLineOption::swap(QCommandLineOption &other)

    Swaps option \a other with this option. This operation is very
    fast and never fails.
*/

/*!
    Returns the names set for this option.
 */
StringList CCommandLineOption::names() const
{
    return d->names;
}

namespace {
    struct IsInvalidName
    {
        typedef bool result_type;
        typedef CString argument_type;

        __declspec(noinline)
        result_type operator()(const CString &name) const 
        {
            if (name.IsEmpty())
                return warn("be empty");

			const TCHAR c = name.GetAt(0);//name.at(0);
            if (c == _T('-'))
                return warn("start with a '-'");
            if (c == _T('/'))
                return warn("start with a '/'");

           
			if (name.Find(_T('=')) != -1)
                return warn("contain a '='");

            return false;
        }

      
		__declspec(noinline) static bool warn(const char *what)
        {
            qWarning("QCommandLineOption: Option names cannot %s", what);
            return true;
        }
    };
} // unnamed namespace

// static
StringList CCommandLineOptionPrivate::removeInvalidNames(StringList nameList)
{
    if (nameList.empty())
        qWarning("QCommandLineOption: Options must have at least one name");
    else
        nameList.erase(std::remove_if(nameList.begin(), nameList.end(), IsInvalidName()),
                       nameList.end());
    return nameList;
}

/*!
    Sets the name of the expected value, for the documentation, to \a valueName.

    Options without a value assigned have a boolean-like behavior:
    either the user specifies --option or they don't.

    Options with a value assigned need to set a name for the expected value,
    for the documentation of the option in the help output. An option with names \c{o} and \c{output},
    and a value name of \c{file} will appear as \c{-o, --output <file>}.

    Call QCommandLineParser::value() if you expect the option to be present
    only once, and QCommandLineParser::values() if you expect that option
    to be present multiple times.

    \sa valueName()
 */
void CCommandLineOption::setValueName(const CString &valueName)
{
    d->valueName = valueName;
}

/*!
    Returns the name of the expected value.

    If empty, the option doesn't take a value.

    \sa setValueName()
 */
CString CCommandLineOption::valueName() const
{
    return d->valueName;
}

/*!
    Sets the description used for this option to \a description.

    It is customary to add a "." at the end of the description.

    The description is used by QCommandLineParser::showHelp().

    \sa description()
 */
void CCommandLineOption::setDescription(const CString &description)
{
    d->description = description;
}

/*!
    Returns the description set for this option.

    \sa setDescription()
 */
CString CCommandLineOption::description() const
{
    return d->description;
}

/*!
    Sets the default value used for this option to \a defaultValue.

    The default value is used if the user of the application does not specify
    the option on the command line.

    If \a defaultValue is empty, the option has no default values.

    \sa defaultValues() setDefaultValues()
 */
void CCommandLineOption::setDefaultValue(const CString &defaultValue)
{
    StringList newDefaultValues;
    
	if (!defaultValue.IsEmpty()) 
	{
		newDefaultValues.push_back(defaultValue);
    }
    // commit:
	d->defaultValues.swap(newDefaultValues);
}

/*!
    Sets the list of default values used for this option to \a defaultValues.

    The default values are used if the user of the application does not specify
    the option on the command line.

    \sa defaultValues() setDefaultValue()
 */
void CCommandLineOption::setDefaultValues(const StringList &defaultValues)
{
    d->defaultValues = defaultValues;
}

/*!
    Returns the default values set for this option.

    \sa setDefaultValues()
 */
StringList CCommandLineOption::defaultValues() const
{
    return d->defaultValues;
}

/*!
    Sets whether to hide this option in the user-visible help output.

    All options are visible by default. Setting \a hide to true for
    a particular option makes it internal, i.e. not listed in the help output.

    \since 5.6
    \sa isHidden
 */
void CCommandLineOption::setHidden(bool hide)
{
    d->hidden = hide;
}

/*!
    Returns true if this option is omitted from the help output,
    false if the option is listed.

    \since 5.6
    \sa setHidden()
 */
bool CCommandLineOption::isHidden() const
{
    return d->hidden;
}
